<?php

namespace app\services;

use plugin\admin\app\model\AppVersion;

class AppVersionService
{
    /**
     * 根据当前版本获取更新信息
     * @param int $currentVersionCode 当前版本号
     * @return array 更新信息，包含是否需要更新及新版本详情
     */
    public static function getUpdateInfo(int $currentVersionCode): array
    {
        // 查询比当前版本号大的最小版本
        $nextVersion = AppVersion::where('version_code', '>', $currentVersionCode)
            ->orderBy('version_code', 'asc')
            ->first();

        if ($nextVersion) {
            return [
                'needUpdate' => true,
                'versionInfo' => [
                    'version_code' => $nextVersion->version_code,
                    'version_name' => $nextVersion->version_name,
                    'update_content' => $nextVersion->update_content,
                    'download_url' => full_url($nextVersion->download_url),
                    'is_force' => (bool)$nextVersion->is_force,
                    'create_time' => $nextVersion->create_time
                ]
            ];
        }

        return [
            'needUpdate' => false,
            'versionInfo' => null
        ];
    }
}